package com.iflytek.crvoice2txt.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iflytek.crvoice2txt.R;
import com.iflytek.crvoice2txt.database.RecordOperation;
import com.iflytek.crvoice2txt.entity.RecorderEntity;
import com.iflytek.crvoice2txt.util.DateUtil;
import com.iflytek.crvoice2txt.util.FileUtil;

import java.util.Date;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RecorderDetailActivity extends BaseActivity implements TextWatcher {

    private final static String TAG = "RecorderDetailActivity";

    private TextView tv_topbar_title, tv_recorder_detail_txttime, tv_recorder_detail_save;

    private ImageView iv_recorder_detail_loading_failed;

    private LinearLayout ll_top_bar_back;

    private RecorderEntity recorderEntity;

    private EditText et_recorder_detail_content, et_recorder_detail_txtname;

    private RecordOperation operation;

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.ll_top_bar_back:
                finish();
                break;
            case R.id.tv_recorder_detail_save:
                saveContentAsyn();
                break;
        }
    }

    @Override
    protected void init() {
        operation = new RecordOperation();
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                recorderEntity = (RecorderEntity) bundle.getParcelable(getResources().getString(R.string.recoder_entity_flag));
            }
        }
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_recorder_detail;
    }

    @Override
    public void initView(View view) {

        tv_recorder_detail_save = findViewById(R.id.tv_recorder_detail_save);
        et_recorder_detail_content = findViewById(R.id.et_recorder_detail_content);
        tv_topbar_title = findViewById(R.id.tv_topbar_title);
        tv_topbar_title.setText("详情");
        ll_top_bar_back = findViewById(R.id.ll_top_bar_back);
        ll_top_bar_back.setVisibility(View.VISIBLE);
        et_recorder_detail_txtname = findViewById(R.id.et_recorder_detail_txtname);
        tv_recorder_detail_txttime = findViewById(R.id.tv_recorder_detail_txttime);
        iv_recorder_detail_loading_failed = findViewById(R.id.iv_recorder_detail_loading_failed);
        if (recorderEntity != null) {
            iv_recorder_detail_loading_failed.setVisibility(View.GONE);
            et_recorder_detail_txtname.setText(recorderEntity.name);
            tv_recorder_detail_txttime.setText(recorderEntity.dateStr);
            setContentAsyn(recorderEntity.txt_path + recorderEntity.name);
        } else {
            iv_recorder_detail_loading_failed.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void setListener() {
        ll_top_bar_back.setOnClickListener(this);
        tv_recorder_detail_save.setOnClickListener(this);
    }

    @Override
    public void doBusiness(Context mContext) {

    }

    private void setContentAsyn(final String path) {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                String str = FileUtil.readFileContentStr(path);
                recorderEntity.text = str;
                subscriber.onNext(str);
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(String str) {
                        Log.i("kokokoko",recorderEntity.type+":");
                        et_recorder_detail_content.setText(str);
                        et_recorder_detail_content.setTextColor(getResources().getColor(recorderEntity.type==1?R.color.A17:R.color.A3));
                    }
                });
    }

    private void saveContentAsyn() {
        if (!recorderEntity.name.equals(et_recorder_detail_txtname.getText().toString()) ||
                !recorderEntity.text.equals(et_recorder_detail_content.getText().toString())) {
            recorderEntity.type = 1;
        }
        recorderEntity.name = et_recorder_detail_txtname.getText().toString();
        Observable.create(new Observable.OnSubscribe<Boolean>() {
            @Override
            public void call(Subscriber<? super Boolean> subscriber) {
                boolean b = FileUtil.writeTxtToFile(et_recorder_detail_content.getText().toString(),
                        recorderEntity.txt_path, recorderEntity.name);
                if (!b) {
                    subscriber.onNext(false);
                    subscriber.onCompleted();
                } else {
                    recorderEntity.size = FileUtil.getFileSize(recorderEntity.txt_path + recorderEntity.name);
                    subscriber.onNext(true);
                    subscriber.onCompleted();
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(Boolean b) {
                        if (b) {
                            Log.i("kokokoko",recorderEntity.toString());
                            operation.updateAsyn(recorderEntity, RecorderDetailActivity.this);
                        } else {
                            showToast("保存失败！");
                        }
                    }
                });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        int length = et_recorder_detail_txtname.length();
        et_recorder_detail_txtname.setSelection(0, length > 4 ? length - 4 : 0);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (operation != null) {
            operation.closeRealm();
        }
    }
}
